package com.sxt.servlet;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.sxt.common.CodeMsg;
import com.sxt.common.Constant;
import com.sxt.common.Result;
import com.sxt.pojo.User;
import com.sxt.service.IUserService;
import com.sxt.service.impl.UserServiceImpl;
import com.sxt.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/user.do")
public class UserServlet extends HttpServlet {
    IUserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tag = req.getParameter("service");
        try {
            Method m = this.getClass().getDeclaredMethod(tag,HttpServletRequest.class,HttpServletResponse.class);
            m.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String checkCode = req.getParameter("checkCode");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //校验验证码
        HttpSession session = req.getSession();
        //获取验证码
        Object o = session.getAttribute(Constant.CHECK_CODE_KEY);
        //校验验证码
        if(o == null || !o.toString().equals(checkCode)){
            Result rs = new Result(CodeMsg.USER_CHECK_CODE_ERROR);
            RespUtil.write(resp,rs);
            return;
        }
        //调查登录的业务方法
        Result rs = userService.login(username,password);
        //登陆成功 则将用户放入session
        if (rs.getCode().equals(CodeMsg.SUCCESS.code)){
            session.setAttribute(Constant.CURRENT_USER_KEY,rs.getData());
            rs.setData(null);
        }
        //向浏览器返回业务数据
        RespUtil.write(resp,rs);
    }


    /**
     * 处理用户列表请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        Integer pageNo = 1;
        Integer pageSize = 10;
        //进行数据的校验
        if (NumberUtil.isNumber(page)){
            pageNo = Integer.parseInt(page) > 0 ? Integer.parseInt(page):pageNo;
        }
        if (NumberUtil.isNumber(limit)){
            pageSize = Integer.parseInt(limit) > 0 ? Integer.parseInt(limit):pageSize;
        }
        String username = req.getParameter("username");
        String realname = req.getParameter("realname");
        String deleted = req.getParameter("deleted");
        String role = req.getParameter("role");
        //存储所有查询参数的容器
        Map<String,String> params = new HashMap<>();
        if (StrUtil.isNotBlank(username)){
            params.put("username",username);
        }
        if (StrUtil.isNotBlank(realname)){
            params.put("realname",realname);
        }
        if (StrUtil.isNotBlank(role)){
            params.put("role",role);
        }
        if (StrUtil.isNotBlank(deleted)){
            params.put("deleted",deleted);
        }
        Result rs = userService.queryPage(params,pageNo,pageSize);
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 处理重置密码的请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void resetPwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Result rs = userService.resetPassword(id);
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 员工离职
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Result rs = userService.delete(id);
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 处理新增员工的请求
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String realname= req.getParameter("realname");
        String username = req.getParameter("username");
        String role = req.getParameter("role");

        Result rs = userService.add(realname,username,role);
        //将数据输出
        RespUtil.write(resp,rs);
    }

    protected void logOut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除session
        req.getSession().invalidate();
        //跳转到登录界面
        resp.sendRedirect("index.jsp");
    }

    /**
     * 获取所有的业务员
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void salesmans(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result rs = userService.queryAllSalesmans();
        //将数据输出
        RespUtil.write(resp,rs);
    }

    /**
     * 修改密码
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void updatePassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //用户输入的密码
        String password = req.getParameter("password");
        //用户新密码
        String newPassword = req.getParameter("newPassword");
        //获取当前用户
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute(Constant.CURRENT_USER_KEY);
        String oldPassword = user.getPassword();
        if (!StrUtil.equals(password,oldPassword)){
            RespUtil.write(resp,new Result(CodeMsg.USER_PASSWORD_ERROR));
            return;
        }
        Result rs = userService.updatePassword(user.getId(),newPassword);
        //将数据输出
        RespUtil.write(resp,rs);
    }



}
